package com.sctvcloud.longchang.ui.adapter.holder;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.widget.ImageView;

import com.gridsum.tracker.GridsumWebDissector;
import com.ruihang.generalibrary.ui.adapter.holder.BaseRecylerHolder;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.DateUtils;
import com.sctvcloud.longchang.R;
import com.sctvcloud.longchang.base.BaseActivity;
import com.sctvcloud.longchang.beans.LiveItem;
import com.sctvcloud.longchang.beans.NewsItem;
import com.sctvcloud.longchang.ui.activities.LivingActivity;
import com.sctvcloud.longchang.ui.adapter.CommonFrgAdapter;
import com.sctvcloud.longchang.ui.utils.GlideUtil;
import com.sctvcloud.longchang.ui.utils.UIUtils;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LiveNewHolder extends BaseAbsHolder<LiveItem> implements View.OnClickListener {
    @BindView(R.id.item_title)
    protected CustomFontTextView title;
    @BindView(R.id.item_time)
    protected CustomFontTextView time;
    @BindView(R.id.item_img)
    protected CustomEXImageView img;
    @BindView(R.id.click_count)
    protected CustomFontTextView clickcount;
    private LiveItem mNewsItem;

    public LiveNewHolder(Context context, View itemView) {
        super(context, itemView);
        itemView.setOnClickListener(this);
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(LiveItem newsItem) {
        mNewsItem = newsItem;
        title.setText(TextUtils.isEmpty(newsItem.getTitle()) ? "" : newsItem.getTitle());
        time.setText(TextUtils.isEmpty(newsItem.getTime()) ? "" : DateUtils.formatDate(DateUtils
                .getDataDate(newsItem), ""));
        clickcount.setText(UIUtils.getReadingNumber(newsItem.getReadCount()));
        clickcount.setVisibility(View.GONE);
        GlideUtil.loadImgLarge169Def(context, newsItem.getLiveImg(), img);
    }


    @Override
    public void onClick(View v) {
        if (mNewsItem != null) {
            Intent intent = new Intent(context, LivingActivity.class);
//		if (liveItem != null && liveItem.isLiving()) {
            intent.putExtra(BaseActivity.EX_URL, mNewsItem.getLiveUrl());
            intent.putExtra(BaseActivity.EX_DATA, mNewsItem.getLiveImg());
            if (mNewsItem.isLiving()) {
                pairs.add(new Pair<String, String>("位置", "正在直播"));
            } else {
                pairs.add(new Pair<String, String>("位置", "直播回顾"));
            }
            ArrayList<Pair<String, String>> pairArrayList = new ArrayList<>();
            pairArrayList.addAll(pairs);
            pairArrayList.add(new Pair<String, String>("行为类型", "点击"));
            if (fragment != null) {
                GridsumWebDissector.getInstance().trackEvent(fragment, "", mNewsItem.getTitle(), "", 200, pairArrayList);
            } else if (activity != null) {
                GridsumWebDissector.getInstance().trackEvent(activity, "", mNewsItem.getTitle(), "", 200, pairArrayList);
            }
            HashMap<String, String> map = new HashMap<>();
            for (Pair<String, String> pair : pairs) {
                map.put(pair.first, pair.second);
            }
            intent.putExtra("TAG", map);
            context.startActivity(intent);
        }
    }
}
